//
//  MVVMViewModel.m
//  Demo_MVVM_RAC
//
//  Created by 陈冰 on 2020/3/8.
//  Copyright © 2020 ChenBing. All rights reserved.
//

#import "MVVMViewModel.h"

@interface MVVMViewModel ()

@property (nonatomic, copy) SuccessBlock successBlock;
@property (nonatomic, copy) FailBlock failBlock;

@end

@implementation MVVMViewModel

- (instancetype)initWithSuccessBlock:(SuccessBlock)aSuccessBlock failBlock:(FailBlock)aFailBlock {
    self = [super init];
    if (self) {
        _successBlock = [aSuccessBlock copy];
        _failBlock = [aFailBlock copy];
    }
    return self;
}

- (void)loadData {
    // 异步加载数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 加载数据
        NSArray *tempArray =
        @[
            @{@"name":@"AAAA",@"imageUrl":@"http://CC",@"num":@"9"},
            @{@"name":@"BBB",@"imageUrl":@"http://James",@"num":@"9"},
            @{@"name":@"CCC",@"imageUrl":@"http://Gavin",@"num":@"9"},
            @{@"name":@"DDD",@"imageUrl":@"http://Cooci",@"num":@"9"},
            @{@"name":@"EEE",@"imageUrl":@"http://Dean ",@"num":@"9"},
            //      @{@"name":@"FFF",@"imageUrl":@"http://CC",@"num":@"9"},
            //      @{@"name":@"GGG",@"imageUrl":@"http://James",@"num":@"9"},
            //      @{@"name":@"QQQ",@"imageUrl":@"http://Gavin",@"num":@"9"},
            //      @{@"name":@"WWW",@"imageUrl":@"http://Cooci",@"num":@"9"},
            //      @{@"name":@"RRR",@"imageUrl":@"http://CC",@"num":@"9"},
            //      @{@"name":@"TTT",@"imageUrl":@"http://James",@"num":@"9"},
            //      @{@"name":@"YYY",@"imageUrl":@"http://Gavin",@"num":@"9"},
            //      @{@"name":@"UUU",@"imageUrl":@"http://Cooci",@"num":@"9"},
            //      @{@"name":@"III",@"imageUrl":@"http://CC",@"num":@"9"},
            //      @{@"name":@"OOO",@"imageUrl":@"http://James",@"num":@"9"},
            //      @{@"name":@"PPP",@"imageUrl":@"http://Gavin",@"num":@"9"},
            //      @{@"name":@"SSS",@"imageUrl":@"http://Cooci",@"num":@"9"},
            //      @{@"name":@"HHH",@"imageUrl":@"http://Dean ",@"num":@"9"}
        ];
        for (NSInteger i = 0; i< tempArray.count; i++) {
            MVVMModel *m = [MVVMModel modelWithDict:tempArray[i]];
            [self.dataArray addObject:m];
        }
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新
            if (self.successBlock) {
                self.successBlock(self.dataArray);
            }            
        });
    });
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

- (void)didClickAddBtnWithNum:(NSString *)num indexPath:(NSIndexPath *)indexPath {
    MVVMModel *m = self.dataArray[indexPath.row];
    m.num = num;
    if (self.successBlock) {
        self.successBlock([self.dataArray copy]);
    }
}
@end
